package com.taptech.doufu.ui.viewholder.personalcenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.NetworkImageView;

/**
 * Created by lsy on 2015/10/8.
 */
public class UserRecyclerViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener {

    Context mContext;
    // 数据
    PersonalCardInfo bean;
    // UI控件
    RelativeLayout backLayout;
    NetworkImageView userHeader;
    ImageView addBtn;
    TextView userName, userLabel, addText;
    View addView;
    ImageView medalImg;

    public UserRecyclerViewHolder(Context context, View itemView) {
        super(context, itemView);
        mContext = context;
        backLayout = (RelativeLayout) itemView.findViewById(R.id.personal_card_back_layout);
        userName = (TextView) itemView.findViewById(R.id.personal_center_user_card_name);
        userLabel = (TextView) itemView.findViewById(R.id.personal_center_user_card_label);
        userHeader = (NetworkImageView) itemView.findViewById(R.id.personal_center_user_card_icon);
        userHeader.setFullScreen(false);
        userHeader.setRound(true);
        addView = itemView.findViewById(R.id.personal_center_user_card_add_view);
        addBtn = (ImageView) itemView.findViewById(R.id.personal_center_user_card_add_btn);
        addText = (TextView) itemView.findViewById(R.id.personal_center_user_card_add_text);
        medalImg = (ImageView) itemView.findViewById(R.id.personal_author_medal);

    }

    @Override
    public void setViewHolderChildViewContent(MineAbstractBean tempbean, int position) {
        bean = (PersonalCardInfo) tempbean;

        userHeader.setImageResource(R.drawable.default_user_portrait);
        backLayout.setOnClickListener(this);
        addView.setOnClickListener(this);
        if (AccountService.getInstance().isLogin() && AccountService.getInstance().getUserUid().equals(bean.getUid())) {
            addView.setVisibility(View.GONE);
        }

        if (bean.getUser_head_img() == null || bean.getUser_head_img().equals("") || bean.getUser_head_img().equals("null")
                || bean.getUser_head_img().equals("http://imgold2.doufu.la/")) {
        } else {
            if (!TextUtils.isEmpty(bean.getUser_head_img()))
                ImageManager.displayImage(userHeader, bean.getUser_head_img(), 0);
        }
        userName.setText(bean.getNickname());
        DiaobaoUtil.setMedalImgView(bean.getMedal(), medalImg);
        userLabel.setText(bean.getUser_signature());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.personal_center_user_card_add_view:
                if (bean.getFollowing() != null) {
                    if (bean.getFollowing().equals("1")) {

                        if (!AccountService.getInstance().isLogin()) {
                            AccountService.getInstance().jumpToLogin();
                            return;
                        }
                        PersonalInfoService.getInstance().cancelAttention(bean.getUid(), new HttpResponseListener() {

                            @Override
                            public void handleResponse(int handleType, HttpResponseObject response) {
                                if (response.getStatus() == 0) {
                                    UIUtil.toastMessage(mContext, "不能做朋友了");
                                    PersonalInfoService.getInstance().delMyAttentions(bean.getUid());
                                    bean.setFollowing("0");
                                    initBtnType(bean);
                                    //?initData(convertView, position);
                                } else {
                                    UIUtil.toastMessage(mContext, "取消关注失败");
                                }

                            }
                        }, (Activity) mContext);


                    } else {

                        if (!AccountService.getInstance().isLogin()) {
                            AccountService.getInstance().jumpToLogin();
                            return;
                        }
                        PersonalInfoService.getInstance().addAttention(bean.getUid(), new HttpResponseListener() {

                            @Override
                            public void handleResponse(int handleType, HttpResponseObject response) {
                                if (response.getStatus() == 0) {
                                    PersonalInfoService.getInstance().addMyAttentions(bean);
                                    UIUtil.toastMessage(mContext, "关注成“攻”");
                                    bean.setFollowing("1");
                                    initBtnType(bean);
                                    //?initData(convertView, position);
                                } else {
                                    UIUtil.toastMessage(mContext, "添加关注失败");
                                }

                            }
                        }, (Activity) mContext);


                    }
                }


                break;
            case R.id.personal_card_back_layout:
                if (!bean.getUid().equals(AccountService.getInstance().getUserUid())) {
                    UserDetailActivity.Companion.startActivity(mContext,bean.getUid());
                }
                break;
        }
    }


    private void initBtnType(PersonalCardInfo info) {
        int imageResId = 0;
        String text = "";
        String uid = info.getUid();
        int type = 0;


        if (info.getFollowed().equals("1") && info.getFollowing().equals("1")) {
            type = 2;
        }
        // 我的粉丝
        else if (info.getFollowed().equals("1")) {
            type = 0;
        }
        // 我关注的
        else if (info.getFollowing().equals("1")) {
            type = 1;
        }
        if (type == 0) {
            text = "关注";
            imageResId = R.drawable.personal_attention_flag;
        }
        if (type == 1) {
            text = "已关注";
            imageResId = R.drawable.personal_attentioned_flag;
        }
        if (type == 2) {
            text = "萌友";
            imageResId = R.drawable.personal_attention_fans_flag;
        }

        addBtn.setImageResource(imageResId);
        addText.setText(text);

    }
}
